package com.mogist.hqc.module.product.adapter

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import cn.jzvd.JzvdStd
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.mogist.hqc.R
import com.mogist.hqc.entitys.response.ProductDetailItemVo
import talex.zsw.basecore.util.RegTool
import talex.zsw.basecore.util.glide.GlideTool
import talex.zsw.basecore.view.recyleview.DividerItemDecoration
import talex.zsw.basecore.view.recyleview.FullyLinearLayoutManager

/**
 * 作用：订单列表
 * 作者：赵小白 email:vvtale@gmail.com  
 * 修改人：
 * 修改时间：
 * 修改备注：
 */
class ProductDetailAdapter(data: ArrayList<ProductDetailItemVo>) : BaseMultiItemQuickAdapter<ProductDetailItemVo, BaseViewHolder>(
        data)
{
    init
    { // 0:Tab  1:图片  2:文字  3:视频  10:行程
        addItemType(0, R.layout.item_product_detail_0)
        addItemType(1, R.layout.item_product_detail_1)
        addItemType(2, R.layout.item_product_detail_2)
        addItemType(3, R.layout.item_product_detail_3)
        addItemType(10, R.layout.item_product_detail_10)
        addItemType(99, R.layout.item_product_detail_99)
        addItemType(100, R.layout.item_product_detail_100)
    }

    override fun convert(helper: BaseViewHolder, item: ProductDetailItemVo)
    {
        when (helper.itemViewType)
        {
            0  -> // Tab
            { // iTvPoint0
                helper.setText(R.id.iTvPoint0, item.content)
            }
            1  -> // 图片
            {
                GlideTool.loadImg(helper.getView(R.id.iImageView1), item.content)
            }
            2  -> // 文字
            {
                helper.setText(R.id.iText2, item.content)
            }
            3  -> // 视频
            {
                val iJzvdStd = helper.getView<JzvdStd>(R.id.iJzvdStd)
                iJzvdStd.setUp(item.videoUrl, "")

                if (RegTool.isEmpty(item.content))
                {
                    Glide.with(mContext)
                            .setDefaultRequestOptions(RequestOptions().frame(0).centerCrop().error(
                                    ColorDrawable(Color.BLACK)).placeholder(ColorDrawable(Color.BLACK)))
                            .load(item.videoUrl)
                            .into(iJzvdStd.thumbImageView)
                }
                else
                {
                    GlideTool.loadImg(iJzvdStd.thumbImageView,
                                      item.content,
                                      ColorDrawable(Color.BLACK))
                }
            }
            10 -> // 行程
            {
                // i10Day i10Tripe
                // iLLTraffic  i10IvTraffic i10Traffic i10TrafficCon
                // iLLScenic i10Scenic i10RecyclerView i10ScenicCon
                // iLLRepast i10Repast
                // iLLHotel i10Hotel
                var traStr = "其他"
                var traIcon = R.drawable.i_traffic_2
                when (item.trip.traffic)
                {
                    "0" ->
                    {
                        traStr = "飞机"
                        traIcon = R.drawable.i_traffic_0
                    }
                    "1" ->
                    {
                        traStr = "高铁动车"
                        traIcon = R.drawable.i_traffic_1
                    }
                    "2" ->
                    {
                        traStr = "大巴"
                        traIcon = R.drawable.i_traffic_2
                    }
                    "3" ->
                    {
                        traStr = "邮轮"
                        traIcon = R.drawable.i_traffic_3
                    }
                }

                helper.setText(R.id.i10Day, "D${item.trip.days}")
                        .setText(R.id.i10Tripe, "${item.trip.startPlace} -> ${item.trip.endPlace}")
                        .setGone(R.id.iLLTraffic, RegTool.isNotEmpty(item.trip.traffic))
                        .setImageResource(R.id.i10IvTraffic, traIcon)
                        .setText(R.id.i10Traffic, traStr)
                        .setText(R.id.i10TrafficCon, item.trip.trafficContext)
                        .setGone(R.id.iLLScenic, RegTool.isNotEmpty(item.trip.scenicContext))
                        .setGone(R.id.i10RecyclerView, RegTool.isNotEmpty(item.trip.scenicPics))
                        .setText(R.id.i10Scenic, item.trip.scenicContext)
                        .setText(R.id.i10ScenicCon, item.trip.scenicContext)
                        .setGone(R.id.iLLRepast, RegTool.isNotEmpty(item.trip.lunch))
                        .setText(R.id.i10Repast, item.trip.lunch)
                        .setGone(R.id.iLLHotel, RegTool.isNotEmpty(item.trip.hotel))
                        .setText(R.id.i10Hotel, item.trip.hotel)
                if (RegTool.isNotEmpty(item.trip.scenicPics))
                {
                    val picAdapter = ProductPicAdapter()
                    val i10RecyclerView = helper.getView<RecyclerView>(R.id.i10RecyclerView)
                    i10RecyclerView.addItemDecoration(DividerItemDecoration(DividerItemDecoration.HORIZONTAL_LIST,
                                                                            R.color.transparent))
                    i10RecyclerView.layoutManager =
                            FullyLinearLayoutManager(mContext,
                                                     LinearLayoutManager.HORIZONTAL,
                                                     false)
                    i10RecyclerView.adapter = picAdapter
                    val pics =
                            item.trip.scenicPics.split("[,，、]".toRegex())
                                    .dropLastWhile { it.isEmpty() }
                                    .toTypedArray()
                    val arrayList: ArrayList<String> = arrayListOf()
                    for (s in pics)
                    {
                        arrayList.add(s)
                    }
                    picAdapter.replaceData(arrayList)
                }
            }
        }
    }
}